<?php

namespace app\admin\controller;

use app\admin\model\Node;
use app\admin\model\Role;
use app\admin\model\RoleNode;
use app\admin\controller\Base;
use think\Request;
use app\admin\model\Role_node;

class AuthNode extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {

        $list = Role::all();

        $data = [];
        foreach ($list as $val){
            $id = $val->getData()['id'];
            $data[$id] = $val->getData();
            $nid = Role_node::allNid($id);
            $data[$id]['node'] = Node::allNode($nid);
        }

        $node = Node::all();
        $nodes = [];
        foreach ($node as $val){
            $nodes[] = $val->getData();
        }


        return view('role/auth', ['data'=>$data, 'nodes'=>$nodes]);
    }



    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $info = input('put.');

        Role_node::where('rid', '=', $id)->delete();
        $result = 1;
        if (count($info ) > 1){
            $role_node = new Role_node();
            $result = $role_node->add($id,$info['node']);
        }

        $data = [];
        //判断
        if ($result > 0){
            $data['status'] = true;
            $data['message'] = '添加成功';
        }else{
            $data['status'] = false;
            $data['message'] = '添加失败';
        }
        return json($data);
    }

}
